package com.lam.code.base;

import cn.hutool.core.util.StrUtil;
import com.lam.code.base.domain.ReturnT;
import com.lam.code.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lam
 * @description 全局异常处理（只处理Controller抛出的异常)
 * @date 2019/10/24 0:14
 */
@RestControllerAdvice
@Slf4j
public class BaseExceptionHandler {
    /**
     * 预定义错误提示信息
     */
    private final static String ERR_MSG_SYSTEM = "系统异常，请联系管理员";

    /**
     * 错误信息最大长度，超过该长度则显示预定义错误提示信息
     */
    private final static int MAX_LENGTH_ERR_MSG = 100;

    //错误页视图，需在静态资源目录下配置/error/500.html
    private ModelAndView ERR_MV = new ModelAndView("/error/500");

    /**
     * 处理响应结果
     * @param e 异常对象
     * @return 返回Object
     */
    private Object handleErr(HttpServletRequest request,Exception e){
        return handleErr(request, e, null);
    }

    /**
     * 处理响应结果
     * @param e 异常对象
     * @return 返回Object
     */
    private Object handleErr(HttpServletRequest request,Exception e , String errMsg){
        log.error(e.getMessage(),e);
        if (ServletUtil.isAjaxRequest(request)){
            if (StrUtil.isNotEmpty(errMsg)){
                return ReturnT.error(errMsg);
            }
            if (StrUtil.length(e.getMessage()) > MAX_LENGTH_ERR_MSG){
                return ReturnT.error(ERR_MSG_SYSTEM);
            }else{
                return ReturnT.error(e.getMessage());
            }
        }else {
            return ERR_MV;
        }
    }

    /**
     * 错误处理
     * @param request 请求对象
     * @param e 异常对象
     * @return 返回Object
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus
    public Object globalHandleException(HttpServletRequest request, Exception e){
        return handleErr(request, e);
    }

    /**
     * 运行时异常处理
     * @param e 异常对象
     * @return Object
     */
    @ExceptionHandler(RuntimeException.class)
    public Object runtimeExceptionHandler(HttpServletRequest request,RuntimeException e){
        return handleErr(request, e);
    }

    /**
     * 认证异常处理
     * @param request 请求对象
     * @param e 异常对象
     * @return Object
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.NON_AUTHORITATIVE_INFORMATION)
    public Object AuthenticationExceptionHandler(HttpServletRequest request,AuthenticationException e) {
        return handleErr(request, e);
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Object AuthorizationExceptionHandler(HttpServletRequest request, AuthorizationException e){
//        return handleErr(request, e, "权限不足");
        return handleErr(request,e);
    }



}
